import TextField = egret.TextField;
/**
 * Created by PC on 2016/1/22.
 */
class MButton extends BaseView {

    private _width:number;
    private _height:number;
    private _desc:string;
    private _bg:egret.Bitmap;
    private _handler:Function;
    private _context:any;

    public constructor(width:number,height:number,desc:string,handler:Function,context:any){
        super();
        this._width = width || 0;
        this._height = height || 0;
        this._desc = desc || "Button";
        this._handler = handler;
        this._context = context;
        this.anchorOffsetX = this._width>>1;
        this.anchorOffsetY = this._height>>1;
    }

    public onInitialize():void {
        this._bg = this.disposer.newScale9Grid("sm_bg_png",this._width,this._height,new egret.Rectangle(10,10,10,10));
        this.addChild(this._bg);
        this._bg.touchEnabled = true;
        this.disposer.addEventListener(this._bg,egret.TouchEvent.TOUCH_TAP,this.onTap,this);

        var descLab = new egret.TextField();
        descLab.text = this._desc;
        descLab.height = this._height;
        descLab.width = this._width;
        descLab.size = 25;
        descLab.textAlign = egret.HorizontalAlign.CENTER;
        descLab.verticalAlign = egret.VerticalAlign.MIDDLE;
        this.addChild(descLab);

    }

    private onTap(e:egret.TouchEvent):void {
        egret.Tween.get(this).to({scaleX:0.9,scaleY:0.9},80).wait(20).to({scaleX:1,scaleY:1},60).call(function(){
            egret.Tween.removeTweens(this);
            if(this._handler){
                this._handler.apply(this._context,[]);
            }
        },this);
    }

}